#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <private/qzipwriter_p.h>
#include <private/qzipreader_p.h>

#include <QStandardPaths>

static void zipWriter(QString);
static void zipReader(QString);

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString location = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);

//    zipWriter(location);
    zipReader(location);
}

void zipWriter(QString path)
{
    QZipWriter *writer = new QZipWriter(path + "/test.zip");
    //添加文件夹
//    writer->addDirectory(path + "/ss");
    //添加文件
    QFile file(path + "/test.txt");
    if (!file.exists())
    {
        return;
    }
    file.open(QIODevice::ReadOnly);
    writer->addFile("test.txt", file.readAll());
    file.close();
    writer->close();
}

void zipReader(QString path)
{
    QZipReader reader(path + "/test.zip");
    reader.extractAll(path);

    QFile file(path);
    file.open(QIODevice::WriteOnly);
    file.write(reader.fileData(QString::fromLocal8Bit("%1").arg(path + "/Desktop")));
    file.close();
    reader.close();
}

MainWindow::~MainWindow()
{
    delete ui;
}
